<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="文章名称:" prop="chapterId">
        <el-select v-model="queryData.chapterId" placeholder="请选择" style="width: 100%" clearable
                   @clear="formClear('chapterId')">
          <el-option :label="item.name" :value="item.id" v-for="(item, index) in chapterList" :key="index"/>
        </el-select>
      </el-form-item>

      <el-form-item label="问题名字:" prop="problemName">
        <el-input v-model="queryData.problemName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('problemName')"/>
      </el-form-item>

      <el-form-item label="上下架状态:" prop="shelfStatus">
        <el-select v-model="queryData.shelfStatus" placeholder="请选择" style="width: 100%" clearable
                   @clear="formClear('shelfStatus')">
          <el-option label="下架" :value="0" />
          <el-option label="上架" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="lookOverEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column type="index" label="序号" align="center"/>
      <el-table-column prop="name" label="文章名称" align="center"/>
      <el-table-column prop="problemName" label="问题名字" align="center"/>
      <el-table-column prop="answer" label="回答内容" align="center" width="400" show-overflow-tooltip/>
      <el-table-column prop="weight" label="权重" align="center"/>
      <el-table-column label="上下架" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.shelfStatus" :active-value="1" :inactive-value="0"
                     @change="switchChange($event, row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="addPeople" label="添加人" align="center" width="150"/>
      <el-table-column prop="createTime" label="添加时间" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="lookOverEvent(row)">查 看</el-button>
          <el-button type="text" @click="deleteEvent(row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
    <el-drawer title="回答信息" :visible.sync="drawer" direction="rtl" :before-close="drawerClose" size="700px"
               destroy-on-close class="drawer-css">
      <CommonProblemForm :entity="entity" v-if="drawer" @close="drawerClose" @refresh="getRequest"></CommonProblemForm>
    </el-drawer>
  </div>
</template>

<script>

  import { mapGetters } from 'vuex'

  export default {
    components: {
      CommonProblemForm: () => import('./CommonProblemForm.vue')
    },
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'commonProblemTitleList',
    data() {
      return {
        chapterList: [],
        tableList: [],
        queryData: {
          shelfStatus: null,
          problemName: null,
          chapterId: null,
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        entity: null,
        drawer: false
      }
    },
    beforeMount() {
      this.getRequest()
      this.getChapterRequest()
    },
    methods: {
      async getChapterRequest() {
        const res = await this.$ajax.get('/wx/commonProblem/chapter/list')
        this.chapterList = res.rows
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 重置事件
       */
      onReset() {
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 上下架
       */
      async switchChange(event, row) {
        await this.$ajax.post('/wx/commonProblem/update', {
          shelfStatus: event,
          id: row.id
        })
        this.getRequest()
        this.$message.success('操作成功!')
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const queryData = JSON.parse(JSON.stringify(this.queryData))
        delete queryData.provinces
        const res = await this.$ajax.get('/wx/commonProblem/backstage/list', { params: queryData })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * 删除事件
       */
      deleteEvent(row) {
        this.$confirm('确认要删除该数据吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/wx/commonProblem/remove?id=${row.id}`)
          this.$message.success('操作成功!')
          this.onSubmit()
        })
      },
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      },
      /**
       * 新增 or 查看按钮事件
       */
      lookOverEvent(row) {
        this.entity = row ? JSON.parse(JSON.stringify(row)) : null
        this.drawer = true
      },
      /**
       * 关闭抽屉
       */
      drawerClose() {
        this.drawer = false
        this.entity = null
      }
    }
  }
</script>

<style scoped>

</style>
